﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace YJKArch.AxisModule
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdWallToAxis : YJKCommandAuto
	{
		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			Autodesk.Revit.DB.Document document = cmdData.Application.ActiveUIDocument.Document;
			if (2 != (int)document.ActiveView.ViewType && 1 != (int)document.ActiveView.ViewType)
			{
				MessageBox.Show("请在平面视图中运行。", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			YJKArch.AxisModule.WallFilter wallFilter = new YJKArch.AxisModule.WallFilter();
			try
			{
				IEnumerable<Reference> enumerable = this.m_uiDoc.Selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element, wallFilter, "请选择墙体：");
				List<Curve> list = new List<Curve>();
				foreach (Reference reference in enumerable)
				{
					Wall wall = this.m_doc.GetElement(reference) as Wall;
					list.Add(WallAssistFunc.GetWallCurve(wall));
				}
				this.CreateMode1(list);
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				return Autodesk.Revit.UI.Result.Failed;
			}
			return base.YJKExcute(cmdData, ref message, elements);
		}

		private void CreateMode1(List<Curve> arInput)
		{
			List<Line> list = new List<Line>();
			List<Arc> list2 = new List<Arc>();
			using (List<Curve>.Enumerator enumerator = arInput.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					Curve curve = enumerator.Current;
					Line line = curve as Line;
					if (line != null)
					{
						list.Add(line);
					}
					Arc arc = curve as Arc;
					if (arc != null)
					{
						list2.Add(arc);
					}
				}
				goto IL_10E;
			}
			IL_68:
			List<Line> list3 = new List<Line>();
			Line line2 = list.First<Line>();
			list.Remove(line2);
			list3.AddRange(list);
			int num = 0;
			bool flag = list3.Count > 0;
			while (flag)
			{
				Line line3 = list3.ElementAt(num);
				Line line4 = CurveAssistFunc.Combine2Line(this.m_uiApp, line2, line3);
				if (line4 != null)
				{
					list3.Remove(line3);
					list.Remove(line3);
					line2 = line4;
					num = 0;
				}
				else
				{
					num++;
				}
				if (num == list3.Count || list3.Count == 0)
				{
					flag = false;
				}
			}
			this.m_doc.YjkNewGrid(line2);
			IL_10E:
			if (list.Count <= 0)
			{
				return;
			}
			goto IL_68;
		}

		private void CreateMode2(List<Curve> arInput)
		{
			List<Curve> list = new List<Curve>();
			list.AddRange(arInput);
			CurveArray curveArray = new CurveArray();
			while (list.Count > 0)
			{
				List<Curve> list2 = new List<Curve>();
				Curve curve = list.First<Curve>();
				list.Remove(curve);
				list2.AddRange(list);
				int num = 0;
				bool flag = list2.Count > 0;
				while (flag)
				{
					Curve curve2 = list2.ElementAt(num);
					Curve curve3 = CurveAssistFunc.Combine2Curve(this.m_uiApp, curve, curve2);
					if (curve3 != null)
					{
						list2.Remove(curve2);
						list.Remove(curve2);
						curve = curve3;
						num = 0;
					}
					else
					{
						num++;
					}
					if (num == list2.Count || list2.Count == 0)
					{
						flag = false;
					}
				}
				curveArray.Append(curve);
			}
			this.m_doc.GetNewGrids(curveArray);
		}
	}
}
